package org.jeecg.modules.quartz.job;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.dingtalk.DingMessageUtils;
import org.jeecg.modules.message.entity.SysMessage;
import org.jeecg.modules.message.entity.SysMessageTemplate;
import org.jeecg.modules.message.service.ISysMessageService;
import org.jeecg.modules.message.service.ISysMessageTemplateService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import io.finer.erp.jeecg.bas.entity.ZrkPushSet;
import io.finer.erp.jeecg.bas.entity.ZzShoporder;
import io.finer.erp.jeecg.bas.service.IZrkPushSetService;
import io.finer.erp.jeecg.bas.service.IZzShoporderService;

/**
 * 
* @ClassName: MessageJob 
* @Description: 监控生产工单，如有新工单则给指定提醒人发送提醒消息 
* @author heckerstone heckerstone@163.com
* @date Sep 2, 2020 4:15:35 PM 
*
 */
public class MessageJob implements Job {
	
	@Autowired
	private IZzShoporderService zzShoporderService;
	
	@Autowired
	private IZrkPushSetService zrkPushSetService;
	
	@Autowired
	private ISysMessageTemplateService sysMessageTemplateService;
	
	@Autowired
	private ISysMessageService sysMessageService;
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
	private ISysBaseAPI sysBaseApi;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
        //查询所有刚添加的订单
		Map<String,Object> columnMap=new HashMap<String,Object>();
		columnMap.put("wo_status", "1");
		Collection<ZzShoporder> collection=zzShoporderService.listByMap(columnMap);
		if(null!=collection&&collection.size()>0) {
			Iterator<ZzShoporder> iterator=collection.iterator();
			while(iterator.hasNext()) {
				ZzShoporder zzShoporder=iterator.next();
				//该工单未推送过消息
				if(zzShoporder.getIsPush()==0) {
					//查询短信模版及提醒人信息
					Collection<ZrkPushSet> col=zrkPushSetService.list();
					if(null!=col&&col.size()>0) {
						Iterator<ZrkPushSet> ite=col.iterator();
						while(ite.hasNext()) {
							ZrkPushSet zps=ite.next();
							if(zps.getMessageTpe().equals("1")) {
								//生产工单信息需要发送钉钉消息提醒任务分配，查询短信模版
								SysMessageTemplate sysMessage=sysMessageTemplateService.getById(zps.getMessageId());
								//替换模版变量
								String messageContent=sysMessage.getTemplateContent();
								//发送消息时内容带着<p></p>,此处需要替换掉
								messageContent=messageContent.replace("${id}", zzShoporder.getId()).replace("${order_name}", zzShoporder.getOrderName()).replace("<p>", "").replace("</p>", "");
								//查询提醒人钉钉ID
								SysUser sysUser=sysUserService.getById(zps.getUserId());
								boolean flag=DingMessageUtils.sendDingMessage(sysUser.getThirdId(), messageContent);
								if(flag) {
									//发送消息成功后,将该任务单状态设置为已推送,防止多次推送（统一消息发送多次在钉钉接口处理的时候会提示143106，该错误码代表发送消息重复，143105:表示企业自建应用每日推送给用户的消息超过上限）
									zzShoporder.setIsPush(1);
									zzShoporderService.updateById(zzShoporder);
									//发送系统消息
									sysBaseApi.sendSysAnnouncement("admin", sysUser.getUsername(),sysMessage.getTemplateName(), messageContent);
								}
							}
						}
					}
				}
			}
		}
	}

}
